package com.heimali.sf;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.heimali.sf.config.CommonConfig;
import com.heimali.sf.config.Constants;

public class SettingActivity extends Activity implements OnClickListener {

	private EditText mPropertyFeeEditText;
	private EditText mDivorceFeeEditText;
	private EditText mDamagesFeeEditText;
	private Button mOKButton;

	private String defaultPropertyFee = Constants.DEFAULT_PROPERTY_FEE;
	private String defaultDamagesFee = Constants.DEFAULT_DAMAGES_FEE;
	private String defaultDivorceFee = Constants.DEFAULT_DIVORCE_FEE;

	CommonConfig config = CommonConfig.getInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_default_fee);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		/* title bar */
		ImageButton leftButton = (ImageButton) findViewById(R.id.left_btn);
		leftButton.setOnClickListener(this);
		((TextView) findViewById(R.id.title_text)).setText(R.string.setting_default_fee);

		mOKButton = (Button) findViewById(R.id.ok_button);
		mOKButton.setOnClickListener(this);
		mPropertyFeeEditText = (EditText) findViewById(R.id.property_fee_setting);
		mDivorceFeeEditText = (EditText) findViewById(R.id.divorce_fee_setting);
		mDamagesFeeEditText = (EditText) findViewById(R.id.damages_fee_setting);
		initFeeText();
	}

	private void initFeeText() {
		defaultPropertyFee = config.getConfig(Constants.SP_DEFAULT_PROPERTY_FEE,
				String.valueOf(Constants.DEFAULT_PROPERTY_FEE));
		defaultDivorceFee = config.getConfig(Constants.SP_DEFAULT_DIVORCE_FEE,
				String.valueOf(Constants.DEFAULT_DIVORCE_FEE));
		defaultDamagesFee = config.getConfig(Constants.SP_DEFAULT_DAMAGES_FEE,
				String.valueOf(Constants.DEFAULT_DAMAGES_FEE));
		mPropertyFeeEditText.setText(String.valueOf(defaultPropertyFee));
		mDivorceFeeEditText.setText(String.valueOf(defaultDivorceFee));
		mDamagesFeeEditText.setText(String.valueOf(defaultDamagesFee));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_btn:
			finish();
			break;
		case R.id.ok_button:
			defaultPropertyFee = mPropertyFeeEditText.getText().toString().trim();
			defaultDivorceFee = mDivorceFeeEditText.getText().toString().trim();
			defaultDamagesFee = mDamagesFeeEditText.getText().toString().trim();
			// TODO 输入校验

			config.setConfig(Constants.SP_DEFAULT_PROPERTY_FEE, defaultPropertyFee);
			config.setConfig(Constants.SP_DEFAULT_DIVORCE_FEE, defaultDivorceFee);
			config.setConfig(Constants.SP_DEFAULT_DAMAGES_FEE, defaultDamagesFee);

			finish();
			break;
		}
	}

}
